package com.sk.bilibili.model.fragment.videoSet;

import com.sk.api.annotations.Autowired;
import com.sk.bilibili.bean.Focus;
import com.sk.bilibili.bean.VideoSet;
import com.sk.bilibili.callBack.RequestCallBack;
import com.sk.bilibili.dto.Page;
import com.sk.bilibili.mapper.CoinMapper;
import com.sk.bilibili.mapper.FocusMapper;
import com.sk.bilibili.mapper.PraiseMapper;
import com.sk.bilibili.mapper.VideoSetMapper;
import com.sk.bilibili.model.BaseRefreshModel;
import com.sk.bilibili.other.JsonMap;
import com.sk.bilibili.utils.RequestUtils;
import com.sk.bilibili.view.fragment.videoSet.IntroductionFragment;

public class IntroductionModel extends BaseRefreshModel<IntroductionFragment> {

    @Autowired
    VideoSetMapper videoSetMapper;
    @Autowired
    FocusMapper focusMapper;
    @Autowired
    PraiseMapper praiseMapper;
    @Autowired
    CoinMapper coinMapper;
    private int page=1;
    private int size=10;

    @Override
    public void loadData() {
        RequestUtils.process(videoSetMapper.page(page, size,null), videoSetPage -> {
            view.addVideoSet(videoSetPage.getRecords());
            page++;
        });
    }

    public void refreshData(Integer videoSetId) {
        RequestUtils.process(videoSetMapper.getVideoSet(videoSetId), videoSet ->
                view.refreshSuccess(videoSet));
        page=1;
        RequestUtils.process(videoSetMapper.page(page, size,null), videoSetPage -> {
            view.setVideoSet(videoSetPage.getRecords());
            page++;
        });
    }

    public void switchFocus(Integer id) {
        RequestUtils.process(focusMapper.switchFocus(id),
                focus -> view.switchFocusSuccess(focus));
    }

    public void praise(Integer id) {
        JsonMap map = new JsonMap();
        map.put("type",0);
        map.put("targetId",id);
        RequestUtils.process(praiseMapper.switchPraise(map));
    }

    public void addCoin(Integer id) {
        JsonMap map=new JsonMap();
        map.put("num",2);
        map.put("type",0);
        map.put("targetId",id);
        RequestUtils.process(coinMapper.addCoin(map), new RequestCallBack<Void>() {
            @Override
            public void success(Void unused) {
                view.addCoinSuccess();
            }
            @Override
            public void error(Throwable throwable) {
                view.addCoinError(throwable.getMessage());
            }
        });
    }

}